/* lexer for a simple math expression language */
%{
/* C header files */
#include <string.h>
#include "expr.h"
#include "expr_parser_types.h"
#include "expr_parser.h"

#define PARM	yyget_extra(yyscanner)

#define YY_INPUT(buffer, res, max_size)				\
do {												\
	if (PARM->pos >= PARM->length)					\
		res = YY_NULL;								\
	else											\
	{												\
		res = PARM->length - PARM->pos;				\
		res > (int)max_size ? res = max_size : 0;	\
		memcpy(buffer, PARM->buf + PARM->pos, res);	\
		PARM->pos += res;							\
	}												\
} while (0)

#define YY_EXTRA_TYPE pcg_parse_parm_t *

%}

%option reentrant bison-bridge
%option noyywrap
%option nounput

%%

([0-9]+)|([0-9]*\.[0-9]+)	{ yylval->dval = atof(yytext); return NUMBER; }
[a-zA-Z_][a-zA-Z0-9_]*		{ yylval->aval = strdup(yytext); return ID; }
\(							return LPAREN;
\)							return RPAREN;
\+							return PLUS;
\-							return MINUS;
\*							return MULT;
\/							return DIV;
\^							return POW;
\,							return COMMA;
\<							return LT;
\>							return GT;
\|\|						return OR;
\&\&						return AND;
\!							return NOT;
[ \t\n]*					/* ignore whitespace */ ;

%%
